package com.eight.login.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.common.pojo.TbMember;
import com.eight.common.vo.Result;
import com.eight.login.dao.StudentDao;
import com.eight.login.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @program: qy8-education1
 * @description:
 * @author: 张亚祥
 * @create: 2023-06-01 18:55
 **/
@RestController
@RequestMapping("/student/member")
@Api("注册验证")
public class MemberController {
    @Resource
    private StudentService studentService;
    @Resource
    private StudentDao studentDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/getYanZhengMa/{mobile}")
    private Result getYanZhengMa(@PathVariable String mobile) throws Exception {
        Result result = studentService.generateAndSendVerifyCode(mobile);
        return Result.success(result);
    }
    @PostMapping("installMember/{mobile}/{password}")
    private Result installMember(@PathVariable String mobile ,@PathVariable String password){

        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        TbMember tbMember1 = studentDao.selectOne(wrapper);
        if(tbMember1!=null){
            return Result.fail("手机号已存在");
        }
        TbMember tbMember = new TbMember();
        tbMember.setId(IdUtil.getSnowflakeNextIdStr());
        tbMember.setMobile(mobile);
        tbMember.setPassword(DigestUtil.sha256Hex(password));
        tbMember.setGmtCreate(LocalDateTime.now());
        tbMember.setGmtModified(LocalDateTime.now());
        tbMember.setIsType(0);
        studentService.installMember(tbMember);
        return Result.success("添加成功");
    }
    @GetMapping("/tuichuLogin")
    @ApiOperation(value = "退出登录")
    public Result loginOut(HttpServletRequest request){
        String token = request.getHeader("homeToken");
        redisTemplate.delete("user::" + token);
        return Result.success();

    }

    @PostMapping("updateMemberPassword/{mobile}/{newPassword}")
    @ApiOperation(value = "修改密码")
    public Result updateMemberPassword(HttpServletRequest httpServletRequest,@PathVariable String mobile, @PathVariable String newPassword){
        String token = httpServletRequest.getHeader("homeToken");
        String json = redisTemplate.opsForValue().get("user::" + token);
        JSONObject jsonObject = JSONObject.parseObject(json);
        String id = (String)jsonObject.get("id");
        studentService.updateMemberPassword(id, mobile,newPassword);
        return Result.success();

    }
}
